package com.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.pay.enums.PayEnums;
import com.pay.enums.PayTypeEnum;
import com.pay.model.Goods;
import com.pay.service.GoodsService;
import com.pay.util.OrdersPaymentCode;
import com.pay.util.PayParameter;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/*****************************************
 *                                       *
 *  @dookay.com Internet make it happen *
 *  ----------- -----------------------  *
 *  dddd  ddddd Internet make it happen  *
 *  o   o     o Internet make it happen  *
 *  k    k    k Internet make it happen  *
 *  a   a     a Internet make it happen  *
 *  yyyy  yyyyy Internet make it happen  *
 *  ----------- -----------------------  *
 *  @dookay.com Internet make it happen *
 *                                       *
 *  面向对象面向君，不负代码不负卿       *
 ****************************************/

/**
 * 商品service
 */

@Service
public class GoodsServiceImpl implements GoodsService {
    //商品初始化
    private static final Map<Integer, Goods> GOODS_MAP = new HashMap<Integer, Goods>();

    static {
        GOODS_MAP.put(1, new Goods(1, "小米手机", "2018071901", 199, new Date(), new Date()));
        GOODS_MAP.put(2, new Goods(2, "红苹果", "2018071902", 199, new Date(), new Date()));
        GOODS_MAP.put(3, new Goods(3, "菠萝", "2018071903", 199, new Date(), new Date()));
        GOODS_MAP.put(4, new Goods(4, "苹果手机", "2018071904", 199, new Date(), new Date()));
        GOODS_MAP.put(5, new Goods(5, "小米电视", "2018071905", 199, new Date(), new Date()));
        GOODS_MAP.put(6, new Goods(6, "男士上衣", "2018071906", 199, new Date(), new Date()));
        GOODS_MAP.put(7, new Goods(7, "男士拖鞋", "2018071907", 199, new Date(), new Date()));
        GOODS_MAP.put(8, new Goods(8, "风行拖拉机", "2018071908", 199, new Date(), new Date()));
        GOODS_MAP.put(9, new Goods(9, "时风三轮车", "2018071909", 199, new Date(), new Date()));
        GOODS_MAP.put(10, new Goods(10, "哈佛H6", "20180719010", 199, new Date(), new Date()));
        GOODS_MAP.put(11, new Goods(11, "华为手机", "20180719011", 199, new Date(), new Date()));
        GOODS_MAP.put(12, new Goods(12, "诺基亚", "20180719012", 199, new Date(), new Date()));
    }

    public Goods findGoodsById(Integer id) {
        Goods goods = GOODS_MAP.get(id);
        return goods;
    }

    public Map<String, Object> payUserApplyByUsernameAndArticleId(String payType, String token, Integer articleId, String name, String mobile, String advancePaymentMoney) throws AlipayApiException, Exception {

        Map<String, Object> result = new HashMap<String, Object>();
        /*
        组装支付信息
         */
        PayParameter parameter = new PayParameter();
        int userId=1;
        BigInteger paySn = this.getPaySn(userId);
        parameter.setPaySn(paySn);
        parameter.setAdvancePaymentMoney(advancePaymentMoney);
        parameter.setOrdersName("商品订单" + paySn);
        parameter.setNotifyUrl("pay/app/ylpay/activity/notify");
        Map<String, Object> paySign = null;

        if (payType.equals(OrdersPaymentCode.ALIPAY)) {
            parameter.setPayType("trade.precreate");
            paySign = PayEnums.YIN_LIAN_PAY.getPaySign(parameter);
            //  paySign = PayEnums.A_LI_PAY.getPaySign(parameter);

        } else if (payType.equals(OrdersPaymentCode.WXPAY)) {
            parameter.setPayType("wx.unifiedOrder");
            paySign = PayEnums.YIN_LIAN_PAY.getPaySign(parameter);
            // paySign = PayEnums.W_X_PAY.getPaySign(parameter);

        } else if (payType.equals(OrdersPaymentCode.YSFPAY)) {
            parameter.setPayType("uac.appOrder");
            paySign = PayEnums.YIN_LIAN_PAY.getPaySign(parameter);

        }
        result.put("payParamString", paySign);
        return result;
    }

    public List<Goods> findListGoods() {
        List<Goods> list = new ArrayList<Goods>();
        for (Map.Entry<Integer, Goods> integerGoodsEntry : GOODS_MAP.entrySet()) {
            Integer key = integerGoodsEntry.getKey();
            Goods value = integerGoodsEntry.getValue();
            if (value != null) {
                list.add(value);
            }
        }
        return list;
    }



    public static BigInteger getPaySn(int memberId) {
        String paySn = Integer.toString((int) Math.round(Math.random() * 89 + 10))
                + String.format("%010d", Integer.parseInt(Long.toString(System.currentTimeMillis() / 1000L)) - 946656000)
                + String.format("%03d", Math.round(Math.random() * 1000))
                + String.format("%03d", memberId % 1000);
        BigInteger integer = new BigInteger(paySn);
        return integer;
    }
}
